package com.hrbxlh.actionImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hrbxlh.dao.MenuDao;
import com.hrbxlh.util.pageUtil;

/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：MenuActionImpl.java
* Description：菜单actionImpl类,负责操作菜单数据以及逻辑处理。
* History：
* Date           Author               Desc
* 2017.1.6		  xzx                 建立
* -------------------------------------------------------
*/

@Component("menuActionImpl")
@Transactional
public class MenuActionImpl {
	
	@Resource(name="menuDao")
	private MenuDao menuDao;
	
	/**
	 * @description 获取全部菜单信息
	 * @param Map<String, Object> 如果有查询条件，就将条件set到map中。要与数据库column一致。
	 * @return List<Map<String,Object>> (菜单集合)
	 * @throws Exception
	 */
	public List<Map<String, Object>> getMenuList(Map<String, Object> paramMap) throws Exception {

		List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();
		try {
			returnList=menuDao.getMenuList(paramMap);
		} catch (Exception e) {
			throw e;
		}
		return returnList;
	}
	
	/**
	 * @description 添加菜单
	 * @param Map<String, Object> key值要与column的值相同。
	 * @return boolean true成功   false失败
	 * @throws Exception
	 */
	public boolean addMenu(Map<String, Object> paramMap) throws Exception {

		boolean flag = false;
		try {
			menuDao.addMenu(paramMap);
			flag = true;
		} catch (Exception e) {
			throw e;
		}
		return flag;
	}
	
	/**
	 * @description 修改菜单
	 * @param Map<String, Object> key值要与column的值相同。
	 * @return boolean true成功   false失败
	 * @throws Exception
	 */
	public boolean saveMenu(Map<String, Object> paramMap) throws Exception {

		boolean flag = false;
		try {
			menuDao.saveMenu(paramMap);
			flag = true;
		} catch (Exception e) {
			throw e;
		}
		return flag;
	}
	
	/**
	 * @description 删除菜单
	 * @param List<Integer> 菜单id集合
	 * @return Map<String, String>，result：true成功，false失败，errorMsg：失败信息。
	 * @throws Exception
	 */
	public Map<String, String> delMenu(List<String> paramList) throws Exception {

		Map<String, String> resultMap = new HashMap<String, String>(); 
		String failMsg = "";
		boolean resultFlag = false;
		//用于存放参数
		Map<String, Object> paramMap = null;
		List<Integer> myParamList = new ArrayList<Integer>();
		try {
			for(int position=0; position < paramList.size(); position++){
				int menuId = Integer.parseInt(paramList.get(position));
				List<Map<String, Object>> roleList = menuDao.getMenuOwnRole(menuId);
				paramMap = new HashMap<String, Object>();
				paramMap.put("menuId", menuId);
				//查询是不是根菜单
				List<Map<String, Object>> menuMessageList = menuDao.getMenuList(paramMap);			
				if(menuMessageList != null && menuMessageList.size() != 0){					
					if((Integer)menuMessageList.get(0).get("parentId") == 0){						
						failMsg = menuMessageList.get(0).get("menuName") + " 菜单为根菜单，不能删除";
						break;
					}
				}
				//查询该菜单是否在某角色的权限信息中
				if(roleList != null && roleList.size()!=0){	
			    	failMsg = menuMessageList.get(0).get("menuName") + " 菜单在角色权限信息中，不能删除";
					break;
			    }else{
			    	myParamList.add(menuId);
			    }
			}
			
			if(myParamList.size()==paramList.size()){
				menuDao.delMenu(myParamList);
				resultFlag = true;
			}
			
			resultMap.put("result", String.valueOf(resultFlag));
			resultMap.put("errorMsg", failMsg);
		} catch (Exception e) {
			throw e;
		}
		return resultMap;
	}
	
	/**
	 * @description 查询菜单列表的方法，分页。
	 * @param .Map<String,Object> paramMap pageNow：当前页数，pageSize：每页多少条数，其他条件与数据库column值一致。
	 * @return Map<String, Object> ，menuList：菜单列表，pageNow：当前页数，totalPage：总页数。
	 * @throws Exception
	 */
	public Map<String, Object> getMenuListByPage(Map<String, Object> paramMap) throws Exception {

		Map<String, Object> returnMap = new HashMap<String, Object>();
		try {
			//获取分页数据
			int pageNow = (Integer)paramMap.get("pageNow");
			int pageSize = (Integer)paramMap.get("pageSize");
			int startNum = pageUtil.getSqlStartPos(pageNow, pageSize);
			paramMap.put("startNum", startNum);
			//获取部门信息
			List<Map<String, Object>> depList = menuDao.getMenuList(paramMap);
			returnMap.put("menuList", depList);
			returnMap.put("pageNow", pageNow);
			
			//获取全部页数
			paramMap.put("pageNow", 0);
			List<Map<String, Object>> allMenuList = menuDao.getMenuList(paramMap);
			int totalPage = pageUtil.getTotalPage(allMenuList, pageSize);
			returnMap.put("totalPage", totalPage);	
		} catch (Exception e) {
			throw e;
		}
		return returnMap;
	}
	
}
